package com.ruoyi.project.zxbd.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.zx.PageData;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.zxbd.mapper.BdPartnerMapper;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客户Service业务层处理
 *
 * @author mn
 * @date 2022-12-30
 */
@Service
public class BdPartnerServiceImpl extends BaseController{
    @Autowired
    private BdPartnerMapper BdPartnerMapper;

    /**
     * 查询客户
     *
     * @param id 客户主键
     * @return 客户
     */
    public PageData selectBdPartnerById(Long id) {
        return BdPartnerMapper.selectBdPartnerById(id);
    }

    /**
     * 查询客户列表
     *
     * @param pd 客户
     * @return 客户
     */
    public List<PageData> selectBdPartnerListPd(PageData pd){
        return BdPartnerMapper.selectBdPartnerListPd(pd);
    }

    /**
     * 新增客户
     *
     * @param pd 客户
     * @return 结果
     */
    public int insertBdPartnerPd(PageData pd) {
//        String mobile=pd.get("mobile").toString();
        PageData pd1 = new PageData();
        pd1.put("mobile",pd.getString("mobile"));
        List<PageData> list = BdPartnerMapper.selectBdPartnerListPd(pd1);
        if (list.size() > 0) {
            PageData result=list.get(0);
            String remark = result.get("remark").toString()+DateUtils.getDate() + ",再次投递";
            result.put("remark",remark);
            return BdPartnerMapper.updateBdPartnerPd(result);
        }
        pd.put("user_id",this.getUserId());
        pd.put("update_by",this.getUsername());
        pd.put("update_time",DateUtils.getTime());
        pd.put("create_by",this.getUsername());
        pd.put("create_time",DateUtils.getTime());
        pd.put("tenant_id",this.getUserTenantId());

        return BdPartnerMapper.insertBdPartnerPd(pd);
    }

    /**
     * 修改客户
     *
     * @param pd 客户
     * @return 结果
     */
    public int updateBdPartnerPd(PageData pd){
//        pd.put("update_time",DateUtils.getNowDate());
        pd.put("update_by",this.getUsername());
        pd.put("update_time",DateUtils.getTime());
        return BdPartnerMapper.updateBdPartnerPd(pd);
    }

    /**
     * 批量删除客户
     *
     * @param ids 需要删除的客户主键
     * @return 结果
     */
    public int deleteBdPartnerByIds(Long[] ids) {
        return BdPartnerMapper.deleteBdPartnerByIds(ids);
    }

    /**
     * 删除客户信息
     *
     * @param id 客户主键
     * @return 结果
     */
    public int deleteBdPartnerById(Long id) {
        return BdPartnerMapper.deleteBdPartnerById(id);
    }

    public int updateSales(Long[] ids,Long id) {
        return BdPartnerMapper.updateSales(ids,id);
    }
}
